#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <stdio.h>
#include <time.h>

#include "Pvcalc.h"

using namespace std;

void usage()
{
     cout <<endl ;
     cout <<"USAGE : " ;
     cout <<"pvalue.exe  Nhit Nmiss Score Accurracy [outfile]" ;
}

int main(int argc, char *argv[])
{
    int   width   = -1   ;
    int   height  = -1   ;
    float score   = -1.0 ;
    int   accuracy= -1   ;
    string outFile= ""   ;
    
    if( argc < 4)
    {
        cout << "not enough arguments ! " << endl ;
        usage();
        return -1;        
    }
    
    for(int i =0 ; i<argc ; i++)
    {
            std::string text( argv[i] );
            std::istringstream ss( text );
            
            //ss >> number;
            if( i==1)
            {
                ss >> width ;                
            }
            if( i==2)
            {
                ss >> height ;                
            }
            if( i==3)
            {
                ss >> score ;                
            }
            if( i==4)
            {
                ss >> accuracy ;                
            }
            if( i == 5 )
            {
                outFile = text ;
            }
            
    }
    
    if(width    <= 0 ) { cout << "width    (="<< width    <<") cannot be 0 or less than 0" << endl ;usage();return -1; }
    if(height   <= 0 ) { cout << "height   (="<< height   <<") cannot be 0 or less than 0" << endl ;usage();return -1; }
    if(accuracy <= 0 ) { cout << "accuracy (="<< accuracy <<") cannot be 0 or less than 0" << endl ;usage();return -1; }
    if(score  < 0 )  { score = -score ; }
        
 
    time_t now; 
    time(&now); 
    int debut = (int) now ;
    if(outFile == "" )printf("[start time  ] %.24s.\n", ctime(&now)); 
     

    Pvcalc pvc( width , height , score , accuracy , (outFile == "" ) );

	//pvc->print();
    
    string ret = pvc.compute();
   	
   	
   	time(&now); 
    int fin = (int) now ;
    if(outFile == "" )printf("\n[end time    ] %.24s.\n", ctime(&now)); 
    if(outFile == "" )printf("[elapsed time] %i\n",  fin-debut ); 
    if(outFile == "" )cout << "\nresult=" << ret ;


    if(outFile != "" )
    {
        // ecriture dans le fichier
        std::ofstream file( outFile.c_str()  );
        file << ret <<endl;
        file.close();
    }

    return EXIT_SUCCESS;
}

 
